package com.hxh.generater.springBoot.simple.gen;

import com.hxh.config.Constants;
import com.hxh.entity.Project;
import com.hxh.generater.springBoot.genBase.GenSystemConfig;
import com.hxh.generater.springBoot.util.ProcessWriteLine;
import com.hxh.util.GenUtil;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: AICode
 * @Package: com.hxh.generater.springBoot.simple.gen
 * @ClassName: GenSimpleSystemConfig
 * @Author: HXH
 * @Description:
 * @Date: 2024/5/21 16:27
 */
public class GenSimpleSystemConfig implements GenSystemConfig {

    private void generationSystemConfig(Project project) {
        try {
            File classFile = GenUtil.newFile(project, "config", "CorsConfig");

            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\config\\CorsConfig");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            Map<String, String> replaceWord = new HashMap<>(1);
            replaceWord.put("GroupId", project.getBackendProject().getProjectGroup());
            ProcessWriteLine processWriteLine = new ProcessWriteLine(replaceWord);
            while ((line = br.readLine()) != null) {
                processWriteLine.writeLine(line, writer);
            }
            br.close();

            writer.close();
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void generationConstantsClass(Project project) {
        try {
            File classFile = GenUtil.newFile(project, "config", "Constants");
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\config\\Constants");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;

            Map<String, String> replaceWord = new HashMap<>(1);
            replaceWord.put("GroupId", project.getBackendProject().getProjectGroup());
            ProcessWriteLine processWriteLine = new ProcessWriteLine(replaceWord);
            while ((line = br.readLine()) != null) {
                processWriteLine.writeLine(line, writer);
            }
            br.close();

            writer.close();
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void generationPaginationConfig(Project project) {
        try {
            File classFile = GenUtil.newFile(project, "config", "PaginationConfig");
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\config\\PaginationConfig");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            Map<String, String> replaceWord = new HashMap<>(1);
            replaceWord.put("GroupId", project.getBackendProject().getProjectGroup());
            ProcessWriteLine processWriteLine = new ProcessWriteLine(replaceWord);
            while ((line = br.readLine()) != null) {
                processWriteLine.writeLine(line, writer);
            }
            br.close();

            writer.close();
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void generationConfigAbout(Project project) {
        generationSystemConfig(project);
        generationConstantsClass(project);
        generationPaginationConfig(project);
    }
}
